package top.tangyh.lamp.care.mapper;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import top.tangyh.basic.base.mapper.SuperMapper;
import top.tangyh.lamp.care.entity.BPatEvent;
import org.springframework.stereotype.Repository;
import top.tangyh.lamp.care.vo.save.EventInputVO;
import top.tangyh.lamp.common.constant.SpecialEventEnum;
import top.tangyh.lamp.common.utils.PDateUtils;

import java.util.Date;
import java.util.List;

import static top.tangyh.lamp.common.constant.CommonConstant.NO_NUMBER;

/**
 * <p>
 * Mapper 接口
 * 特殊事件表
 * </p>
 *
 * @author zqb
 * @date 2024-07-22 16:36:14
 * @create [2024-07-22 16:36:14] [zqb] [代码生成器生成]
 */
@Repository
public interface BPatEventMapper extends SuperMapper<BPatEvent> {

    default BPatEvent selectOneByEventName(String eventName, String hospitalNumber) {
        LambdaQueryWrapper<BPatEvent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BPatEvent::getEventName, eventName)
                .eq(BPatEvent::getHospitalNumber, hospitalNumber)
                .eq(BPatEvent::getIsDeleted, NO_NUMBER);
        return this.selectOne(queryWrapper);
    }

    default List<BPatEvent> selectByHospitalNumber(String hospitalNumber) {
        LambdaQueryWrapper<BPatEvent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BPatEvent::getHospitalNumber, hospitalNumber)
                .eq(BPatEvent::getIsDeleted, NO_NUMBER);
        return this.selectList(queryWrapper);
    }

    default List<BPatEvent> selectByEventName(String eventName, String hospitalNumber) {
        LambdaQueryWrapper<BPatEvent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BPatEvent::getEventName, eventName)
                .eq(BPatEvent::getHospitalNumber, hospitalNumber)
                .eq(BPatEvent::getIsDeleted, NO_NUMBER);
        return this.selectList(queryWrapper);
    }

    default void updatePatEvetById(EventInputVO eventInputVO, String eventName) {
        LambdaQueryWrapper<BPatEvent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BPatEvent::getId, eventInputVO.getId());
        BPatEvent spEventPO = new BPatEvent();
        spEventPO.setEventName(eventName);
        spEventPO.setEventTime(PDateUtils.getDateTimeOfTimestamp(eventInputVO.getEventTime()));
        this.update(spEventPO, queryWrapper);
    }

    List<BPatEvent> selectEventListByParam(String hospitalNumber, String startTime, String endTime, String icuCode);


    /**
     * @Description: 用于血压这块的事件 取分组后时间最早的事件
     * @Param:
     * @return:
     * @Author: Mr.zqb
     * @Date: 2022/2/18
     */
    List<BPatEvent> selectEventListForBloodPressure(String hospitalNumber, String startTime, String endTime);

    /**
     *根据时间统计死亡人数
     *
     * @return
     */
    default int countDie(Date start, Date end) {
        LambdaQueryWrapper<BPatEvent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BPatEvent::getIsDeleted, NO_NUMBER)
                .eq(BPatEvent::getEventName, SpecialEventEnum.DIE.getEventName())
                .ge(null != start,BPatEvent::getEventTime,start)
                .le(null != end,BPatEvent::getEventTime,end);
        return Math.toIntExact(this.selectCount(queryWrapper));
    }


}


